package cfca.paperless.client.servlet._7_remote;

import java.io.File;

import org.apache.commons.io.FileUtils;

import cfca.paperless.client.bean.SealCertBean;
import cfca.paperless.client.bean.SealInfoBean;
import cfca.paperless.client.servlet.PaperlessConfig;
import cfca.paperless.client.servlet.PaperlessManagerClient;
import cfca.paperless.client.util.Base64;
import cfca.paperless.client.util.GUID;
import cfca.paperless.client.util.IoUtil;
import cfca.paperless.client.util.StringUtil;


public class AutoMakeSealRemoteTest01 {

    public static void main(String[] args) throws Exception {
        try {
            PaperlessManagerClient clientBean = new PaperlessManagerClient(PaperlessConfig.managerUrl, 10000, 60000);// 无纸化管理接口的访问URL

            byte[] sealImage = FileUtils.readFileToByteArray(new File("./TestData/handwriting.png"));
            
            String customerType = "1";//1:个人；2：企业
            String keyAlg = "RSA";
            String keyLength = "2048";
            String userName = "郴州市苏仙区栖凤";
            String identificationType = "0";
            String identificationNo = "aaaa44545455445440";
            SealCertBean sealCertBean = new SealCertBean(customerType, keyAlg, keyLength, userName, identificationType, identificationNo);
            String sealCertXml = sealCertBean.toString();
            
            String sealCode = "rm" + GUID.generateId().substring(20, 30);
            String sealPassword = new String(Base64.encode("11111111","utf-8"));
            System.out.println(sealCode);
            String sealName = "印章";
            String sealPerson = "印章";
            String sealOrg = "";
            SealInfoBean sealInfoBean = new SealInfoBean(sealCode, sealPassword, sealName, sealPerson, sealOrg);
            sealInfoBean.setInvokeWay(2);//远程印章
            String sealInfoXml = sealInfoBean.toString();

            // 操作员编码或者机构号
            String operatorCode = PaperlessConfig.operatorCode;

            // 取得转换后的PDF文件数据
            String result = clientBean.autoMakeSeal(sealImage, sealCertXml, sealInfoXml, operatorCode);

            String code = StringUtil.getNodeText(result, "Code");

            if ("200".equals(code)) {
                System.out.println(result);
                System.out.println("OK:autoMakeSeal");
            } else {
                System.out.println("NG:autoMakeSeal");
                // 处理结果为异常，打印出错误码和错误信息
                System.out.println(result);
            }
            

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
